from google.appengine.ext import db
import os
from google.appengine.ext.webapp import template

import cgi
import wsgiref.handlers

from google.appengine.api import users
from google.appengine.ext import webapp
import cgitb; cgitb.enable()

class Profile(db.Model):
  profileOwner = db.UserProperty() 
  firstName = db.StringProperty()
  lastName = db.StringProperty()
  birthDate = db.StringProperty()
  gender = db.StringProperty()
  maritalStatus = db.StringProperty()
  country = db.StringProperty()
  state = db.StringProperty()
  city = db.StringProperty()
  pin = db.StringProperty()
  presentAddress = db.TextProperty()
  permanentAddress = db.TextProperty()
  emailAddress = db.EmailProperty()
  mobileNumber = db.StringProperty()
  landlineNumber = db.StringProperty()
  idealMatch = db.TextProperty()
  aboutYourself = db.TextProperty()
  lookingFor = db.TextProperty()
  dateOfCreation = db.DateTimeProperty(auto_now_add=True)

  
class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

  
class MainPage(webapp.RequestHandler):
  def get(self):
    greetings = Greeting.all().order('-date')

    if users.get_current_user():
      url = users.create_logout_url(self.request.uri)
      url_linktext = 'Logout'
    else:
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'

    
    template_values = {
      'greetings': greetings,
      'url': url,
      'url_linktext': url_linktext,
      }

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))
  
class ProfilePage(webapp.RequestHandler):
  def get(self):
    greetings = Greeting.all().order('-date')
    createProfileLink = 'Create Profile'
    if not users.get_current_user():
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'
      self.redirect(url)  
    
    else:
      myProfile = db.GqlQuery("SELECT * FROM Profile WHERE profileOwner = :1",users.get_current_user())
      if myProfile.count() < 1:
        createProfileLink = 'Create Profile'
        createProfileUrl = 'createProfile'
      else: 
        createProfileLink = 'Update Profile'
        createProfileUrl = 'updateProfile'        
      url = users.create_logout_url('/')
      url_linktext = 'Logout'
      template_values = {
        'createProfileLink': createProfileLink,  
        'createProfileUrl': createProfileUrl,          
        'myProfile': myProfile,
        'greetings': greetings,
        'url': url,
        'url_linktext': url_linktext,
        }
      path = os.path.join(os.path.dirname(__file__), 'profile.html')
      self.response.out.write(template.render(path, template_values)) 
      

class CreateProfilePage(webapp.RequestHandler):
  def get(self):
    greetings = Greeting.all().order('-date')

    if not users.get_current_user():
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'
      self.redirect(url)  
    
    else:
      url = users.create_logout_url('/')
      url_linktext = 'Logout'
      template_values = {
        'greetings': greetings,
        'url': url,
        'url_linktext': url_linktext,
        }
      path = os.path.join(os.path.dirname(__file__), 'createProfile.html')
      self.response.out.write(template.render(path, template_values))        

class UpdateProfilePage(webapp.RequestHandler):
  def get(self):
    greetings = Greeting.all().order('-date')

    if not users.get_current_user():
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'
      self.redirect(url)  
    
    else:
      myProfile = db.GqlQuery("SELECT * FROM Profile WHERE profileOwner = :1",users.get_current_user())
      url = users.create_logout_url('/')
      url_linktext = 'Logout'
      template_values = {
        'myProfile': myProfile,
        'greetings': greetings,
        'url': url,
        'url_linktext': url_linktext,
        }
      path = os.path.join(os.path.dirname(__file__), 'updateProfile.html')
      self.response.out.write(template.render(path, template_values))      
    
    
class Guestbook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/')

class PersonsList(webapp.RequestHandler):
  def post(self):
    if not users.get_current_user():
        loginURL = users.create_login_url('/createProfile')
        self.redirect(loginURL)  
    else:
      profiles = db.GqlQuery("SELECT * FROM Profile WHERE profileOwner = :1",users.get_current_user())
      if profiles.count() < 1:
        profile = Profile()  
      else: 
        for pr in profiles:
          profile = pr
          break
      profile.profileOwner =  users.get_current_user()
      profile.firstName =  self.request.get('firstName')
      profile.lastName = self.request.get('lastName')
      profile.birthDate = self.request.get('birthDate')
      profile.gender = self.request.get('gender')
      profile.maritalStatus = self.request.get('maritalStatus')
      profile.country = self.request.get('country')
      profile.state = self.request.get('state')
      profile.city = self.request.get('city')
      profile.pin = self.request.get('pin')
      profile.presentAddress = self.request.get('presentAddress')
      profile.permanentAddress = self.request.get('permanentAddress')
      profile.emailAddress = self.request.get('emailAddress')
      profile.mobileNumber = self.request.get('mobileNumber')
      profile.landlineNumber = self.request.get('landlineNumber')
      profile.idealMatch = self.request.get('idealMatch')
      profile.aboutYourself = self.request.get('aboutYourself')
      profile.lookingFor = self.request.get('lookingFor')
      db.put(profile)
      self.redirect('/profile') 
    
    
def main():
  application = webapp.WSGIApplication(
                                       [('/', MainPage),
                                        ('/profile', ProfilePage),
                                        ('/createProfile', CreateProfilePage),
                                        ('/updateProfile', UpdateProfilePage),
                                        ('/sign', PersonsList),
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
